﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace SMTPLibrary
{
    public class SMTPLibrary
    {
        private SmtpClient smtp;
        private MailAddress fromAddress;
        private string fromPassword;


        /// <summary>
        /// establece conexion con el servidor de mail
        /// </summary>
        /// <param name="fromAddress">mail del remitente</param>
        /// <param name="password"></param>
        public SMTPLibrary(string fromAddress, string fromPassword)
        {
            this.fromAddress = new MailAddress(fromAddress, fromAddress);
            this.fromPassword = fromPassword;

            smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(this.fromAddress.Address, fromPassword)
            };
        }

        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="to">mail del destinatario</param>
        /// <param name="subject">titulo del mensaje a enviar</param>
        /// <param name="body">contenido del mensaje</param>
        /// <returns>true si se envio el mensaje</returns>
        public bool Send(string to, string subject, string body)
        {
            var toAddress = new MailAddress(to, to);

            using (var message = new MailMessage(fromAddress, toAddress)
                                 {
                                     Subject = subject,
                                     Body = body
                                 })
            {
                smtp.Send(message);
                return true;
            }
        }//Send


        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            smtp.Dispose();
        }

    }//SMTPLibrary
}
